package com.logistics.reception.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.logistics.erp.entity.domain.*;
import com.logistics.erp.entity.model.*;
import com.logistics.erp.service.ErpOrderService;
import com.logistics.utils.bean.UserBean;
import com.logistics.utils.constants.Constants;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.tool.StringUtils;
import com.logistics.utils.tool.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class ReceptionService {

    @Autowired
    private ErpOrderService erpOrderService;

    @Autowired
    private UserUtils userUtils;

    /**
     * 查询物料信息
     * @param taskNo 任务单号
     * @return
     */
    @DS("erp")
    @Transactional(rollbackFor = {MessageException.class})
    public MaterialReception materialFlow(String taskNo) throws MessageException {
        if (!taskNo.startsWith(Constants.TASK_NO_PREFIX)) {
            throw new MessageException("任务单号不符合规则");
        }
        PacOptransBillDetail billDetail = erpOrderService.materialByTaskNo(taskNo);
        if (StringUtils.isNull(billDetail)) {
            throw new MessageException("物料信息以及提出外协的部门为空");
        }

        //1.判断质检是否需要质检
        if (!StringUtils.isEquals(billDetail.getItemName(), "简单管段") && !StringUtils.isEquals(billDetail.getSendDeptCodeWc(),"2800")){
            //需要质检
            System.out.println("需要质检");
            QcWxDetailV qcWxDetailV = erpOrderService.receptionQuality(taskNo);
            if (StringUtils.isNull(qcWxDetailV)) {
                throw new MessageException("该任务还没有质检!");
            }else {
                //未质检数量
                BigDecimal inspect = qcWxDetailV.getTransferQty().subtract(qcWxDetailV.getSumqty());
                if (inspect.compareTo(BigDecimal.ZERO) == 0){//全部质检完成
                    erpOrderService.updateIfYes(taskNo);
                }else {
                    throw new MessageException("任务号"+ taskNo +"还有"+ inspect +"件没有质检合格!");
                }
            }
        }
        //2.判断任务是否领料齐全
        String OrderNo = erpOrderService.pickingComplete(taskNo);
        if (StringUtils.isNotNull(OrderNo)){
            throw new MessageException("该任务有物料未领!");
        }
        MaterialReception reception = erpOrderService.materialReception(taskNo);
        if (StringUtils.isNull(reception)){
            throw new MessageException("无物料信息");
        }
        //验证是否记账成功
        List<String> status = erpOrderService.statusByOrderNo(taskNo);
        if (StringUtils.isNotNull(status) && status.size() == 1 && StringUtils.isEquals(status.get(0),"Y")){
            reception.setFlag("C");
        }
        return reception;
    }

    /**
     * 外协接收
     * @param erpReceptionSave
     * @throws MessageException
     */
    @DS("erp")
    @Transactional(rollbackFor = {MessageException.class})
    public String save(ErpReceptionSave erpReceptionSave) throws MessageException {
        UserBean userBean = userUtils.getUserInfo();
        erpReceptionSave.setPiUserUniqueId(userBean.getErpUniqueId());
        //存储过程
        try {
            erpOrderService.callMaterialErpReception(erpReceptionSave);
        }catch (Exception e){
            e.printStackTrace();
            throw new MessageException("工序外协交接失败!");
        }

        //验证是否记账成功
        List<String> status = erpOrderService.statusByOrderNo(erpReceptionSave.getPiOrderNoOne());
        if (StringUtils.isNull(status) || status.size() > 1 || StringUtils.isEquals(status.get(0),"N")){
            String errMsg = erpOrderService.findErrMsg(userBean.getErpUniqueId());
            throw new MessageException("记帐没有完全通过,"+errMsg);
        }

        try {
            erpOrderService.callMaterialErpInware(erpReceptionSave.getPiOrderNoOne());
        }catch (Exception e){
            e.printStackTrace();
            throw new MessageException("工序外协交接失败!");
        }

        String sourceMethod = erpOrderService.getsourceMethod(erpReceptionSave.getPiOrderNoOne());

        if (!StringUtils.isEquals("外协回厂",sourceMethod) || !StringUtils.isEquals("外协回厂直流",sourceMethod)
                || !StringUtils.isEquals("外协完工入库",sourceMethod)){
            throw new MessageException("业务类型不正确");
        }

        return sourceMethod;
    }

    /**
     * 入库打印信息查询
     * @param code
     * @return
     */
    public ErpDeliveryOrderDetail inbound(String code) throws Exception{
        ErpDeliveryOrderDetail orderDetail = erpOrderService.inbound(code);
        if (StringUtils.isNull(orderDetail)){
            throw new MessageException("暂无入库信息");
        }
        orderDetail.setFlag(true);
        return orderDetail;
    }

    /**
     * 打印查询任务类型
     * @param code
     * @return
     */
    public String print(String code) {
        return erpOrderService.getsourceMethod(code);
    }
}